package com.project_test.kun.travelling_1st.HomePage.Util;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 
 * 1.网络请求方法, 返回String 2.解析JSON数据,返回List<Map<String,String>>
 * 
 */
public class Tools {


	/**
	 * 给个路劲返回网络请求结果
	 * 
	 * @param path
	 * @return
	 */
	public static String getString(String path) {

		ByteArrayOutputStream outputStream = null;
		String res="";

		try {
			URL url = new URL(path);
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			conn.setReadTimeout(5000); // 设置读取超时时间
			if (conn.getResponseCode() == 200) {
				InputStream inputStream = conn.getInputStream();
				outputStream = new ByteArrayOutputStream();
				byte[] buffer = new byte[8 * 1024];
				int len = 0;
				while ((len = inputStream.read(buffer)) != -1) {
					outputStream.write(buffer, 0, len);
					outputStream.flush();
				}
				// 返回结果
				res=outputStream.toString();

				return res;
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			// 最后要关流
			if (outputStream != null) {
				try {
					outputStream.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}

		return res;
	}
	/**
	 * 解析Json
	 * @param json
	 * @return List<Map> 
	 */
	public static List<Map<String, Object>> parserJson(String json){
		List<Map<String, Object>>  list =  new ArrayList<Map<String,Object>>();
		try {
			JSONObject jsonObject=new JSONObject(json);
			JSONObject object = jsonObject.getJSONObject("data");
			JSONArray dataList=object.getJSONArray("list");
			for (int i = 0; i < dataList.length(); i++) {
				Map<String, Object> map = new HashMap<String, Object>();
				//根据index获取JsonOBJECT
				JSONObject jsonObject1 = dataList.getJSONObject(i);
				map.put("title",  jsonObject1.getString("title"));
				map.put("price",jsonObject1.getString("price"));
				map.put("head_pic",jsonObject1.getString("head_pic"));
				map.put("service_type_name",jsonObject1.getString("service_type_name"));
				map.put("scores",jsonObject1.get("scores"));
				map.put("id",jsonObject1.get("id"));
				list.add(map);
			}
			return list; //返回数据即可
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	/**
	 * 给个URL返回Bitmap
	 * @param path
	 * @return
	 */
	public static Bitmap getBitmap(String path) {

		ByteArrayOutputStream outputStream = null;
		try {
			URL url = new URL(path);
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			conn.setReadTimeout(5000); // 设置读取超时时间
			if (conn.getResponseCode() == 200) {
				InputStream inputStream = conn.getInputStream();
				outputStream = new ByteArrayOutputStream();
				byte[] buffer = new byte[8 * 1024];
				int len = 0;
				while ((len = inputStream.read(buffer)) != -1) {
					outputStream.write(buffer, 0, len);
					outputStream.flush();
				}
				// 返回结果
				return BitmapFactory.decodeByteArray(outputStream.toByteArray(),
						0,outputStream.toByteArray().length);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			// 最后要关流
			if (outputStream != null) {
				try {
					outputStream.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		return null;
	}
}